/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easybooking.clients.gui.handler;

import easybooking.clients.gui.CustomerCreateDialog;

import easybooking.clients.gui.CustomerEditDialog;
import easybooking.clients.gui.CustomerForm;
import easybooking.clients.gui.EBInterface;
import easybooking.clients.gui.EBInterfaceClient;
import easybooking.data.Customer;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author tim
 */
public class CustomerHandler {

    private EBInterface ebi;
    private EBInterfaceClient ebic;
    private CustomerForm cf = new CustomerForm(ebi);
    private CustomerCreateDialog ccd = new CustomerCreateDialog(ebi);;
    private CustomerEditDialog ced = new CustomerEditDialog(ebi);

    public CustomerHandler (EBInterface ebi, EBInterfaceClient ebic){
        this.ebi = ebi;
        this.ebic = ebic;
    }


     public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("createCustomer")) {


            ccd.createAndShowDialog();
        }

        if (e.getActionCommand().equals("customerCreateDialogOk")) {

            // validate input of the zip field
            try {

                int x = Integer.parseInt(ccd.getZip());
            } catch (NumberFormatException nFE) {
                JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
            }

            // adds a new customer example data
            ebic.createCustomer(ccd.getFirstName(), ccd.getLastName(), ccd.getStreet(), ccd.getZip(), ccd.getCity());
            // gets and sets the whole table data
            cf.setTableData(ebic.getAllCustomers());
            // cleans the view
            cf.removeAll();
            // renews the cf
            cf.createAndShowForm();
            // shows the cf again
            ebi.setRightPanel(cf);
            // closes the ccd
            ccd.dispose();
        }
        if (e.getActionCommand().equals("editCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                JOptionPane.showMessageDialog(null, "Editing: " + c.getFirstname() + " " + c.getLastname());

                // pases the customer which should be edited
                ced.createAndShowDialog(c);
            }
        }

        if (e.getActionCommand().equals("customerEditDialogOk")) {

            // validate input of the zip field
            try {

                int x = Integer.parseInt(ced.getZip());
            } catch (NumberFormatException nFE) {
                JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
            }

            // adds a new customer example data
            ebic.updateCustomer(ced.getFirstName(), ced.getLastName(), ced.getStreet(), ced.getZip(), ced.getCity(), ced.getEditableCustomer());
            // gets and sets the whole table data
            cf.setTableData(ebic.getAllCustomers());
            // cleans the view
            cf.removeAll();
            // renews the cf
            cf.createAndShowForm();
            // shows the cf again
            ebi.setRightPanel(cf);
            // closes the ccd
            ced.dispose();
        }

        if (e.getActionCommand().equals("deleteCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                JOptionPane.showMessageDialog(null, "Deleting: " + c.getFirstname() + " " + c.getLastname());
                ebic.deleteCustomer(c);

                cf.setTableData(ebic.getAllCustomers());
                // cleans the view
                cf.removeAll();
                // renews the cf
                cf.createAndShowForm();
                // shows the cf again
                ebi.setRightPanel(cf);
            }
        }

        
       
     }

}